package com.redoor.app.stake.configurer;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * Swagger 配置文件
 *
 * Created by Floki on 2017/8/10.
 */
@Configuration
@EnableSwagger2
public class SwaggerApiConfigurer extends WebMvcConfigurerAdapter {

    /**
     * 注册 swagger-ui.html
     *
     * @param registry
     */
    @Value("${swagger.enable}")
    private boolean enableSwagger;

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
    }

    @Bean
    public Docket createAuthRestApi() {
        return new Docket(DocumentationType.SWAGGER_2).groupName("用户管理")
                .apiInfo(apiInfo("用户管理服务提供的 RestFul APIs"))
                .enable(enableSwagger)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.redoor.app.stake.auth.controller"))
                .paths(PathSelectors.any())
                .build();
    }

    @Bean
    public Docket createBasicDataRestApi() {
        return new Docket(DocumentationType.SWAGGER_2).groupName("基础数据管理")
                .apiInfo(apiInfo("基础数据管理服务提供的 RestFul APIs"))
                .enable(enableSwagger)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.redoor.app.stake.basicdata.controller"))
                .paths(PathSelectors.any())
                .build();
    }

    @Bean
    public Docket createWorkFlowRestApi() {
        return new Docket(DocumentationType.SWAGGER_2).groupName("工单管理")
                .apiInfo(apiInfo("工单管理服务提供的 RestFul APIs"))
                .enable(enableSwagger)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.redoor.app.stake.workflow.controller"))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo(String description) {// 创建API的基本信息，这些信息会在Swagger UI中进行显示
        return new ApiInfoBuilder()
                .title("使用 Swagger2 构建的 RestFul APIs")// API 标题
                .description(description)// API描述
                .version("1.0.0")// 版本号
                .build();
    }
}
